package com.fr.data.impl;

import com.fr.base.FCloneable;
import com.fr.base.StringUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.AbstractTableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.core.define.DataSource;
import com.fr.data.core.define.XMLColumnNameDateType;
import com.fr.data.core.define.XMLColumnNameType;
import com.fr.util.Utils;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class XMLTableData extends AbstractTableData
  implements XMLable
{
  private static final long serialVersionUID = 1L;
  private DataSource dataSource = null;
  protected List row_list = null;
  private String[] xPath = null;
  private String charSet = "UTF-8";
  private XMLColumnNameType[] columns = new XMLColumnNameType[0];
  public static final int COLUMN_TYPE_STRING = 0;
  public static final int COLUMN_TYPE_NUMBER = 1;
  public static final int COLUMN_TYPE_DATE = 2;
  public static final int COLUMN_TYPE_BOOLEAN = 3;

  public String[] getXPath()
  {
    return this.xPath;
  }

  public void setXPath(String[] paramArrayOfString)
  {
    this.xPath = paramArrayOfString;
  }

  public String getCharSet()
  {
    return this.charSet;
  }

  public void setCharSet(String paramString)
  {
    this.charSet = paramString;
  }

  public XMLColumnNameType[] getColumns()
  {
    return this.columns;
  }

  public void setColumns(XMLColumnNameType[] paramArrayOfXMLColumnNameType)
  {
    this.columns = paramArrayOfXMLColumnNameType;
  }

  public int getColumnCount()
    throws TableDataException
  {
    return this.columns.length;
  }

  public DataSource getDataSource()
  {
    return this.dataSource;
  }

  public void setDataSource(DataSource paramDataSource)
  {
    this.dataSource = paramDataSource;
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    if ((paramInt < 0) || (paramInt >= this.columns.length))
      return null;
    String str = (this.columns[paramInt] == null) ? null : this.columns[paramInt].getName();
    return str;
  }

  public int getRowCount()
    throws TableDataException
  {
    init();
    return this.row_list.size();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    init();
    if ((paramInt1 < 0) || (paramInt1 >= this.row_list.size()) || (paramInt2 < 0) || (paramInt2 >= this.columns.length))
      return null;
    return ((Object[])this.row_list.get(paramInt1))[paramInt2];
  }

  protected void init()
    throws TableDataException
  {
    if (this.row_list != null)
      return;
    checkNameAndType();
    try
    {
      InputStream localInputStream = getDataSourceStream();
      readerXMLSource(localInputStream);
      localInputStream.close();
    }
    catch (Exception localException)
    {
      throw new TableDataException(localException.getMessage(), localException);
    }
  }

  protected InputStream getDataSourceStream()
    throws Exception
  {
    return this.dataSource.getSourceStream(getParameters());
  }

  protected void readerXMLSource(InputStream paramInputStream)
    throws Exception
  {
    InputStreamReader localInputStreamReader = new InputStreamReader(paramInputStream, this.charSet);
    readXMLSource(localInputStreamReader);
  }

  protected void readXMLSource(Reader paramReader)
    throws Exception
  {
    XMLableReader localXMLableReader = XMLableReader.createXMLableReader(paramReader);
    if (localXMLableReader != null)
    {
      this.row_list = new ArrayList();
      localXMLableReader.readXMLObject(new ReadXMLLayer(this, 0));
    }
  }

  protected void checkNameAndType()
    throws TableDataException
  {
    for (int i = 0; i < this.columns.length; ++i)
    {
      if (this.columns[i] == null)
        throw new TableDataException("Column index:" + i + " is null");
      if (StringUtils.isBlank(this.columns[i].getName()))
        throw new TableDataException("ColumnName index:" + i + " is null");
      if ((this.columns[i].getType() == 2) && (!(this.columns[i] instanceof XMLColumnNameDateType)))
        throw new TableDataException("ColumnType index:" + i + " is not a date");
    }
  }

  private static Object convertStringValue(String paramString, XMLColumnNameType paramXMLColumnNameType)
    throws Exception
  {
    if (paramXMLColumnNameType.getType() == 0)
      return paramString;
    if (paramXMLColumnNameType.getType() == 1)
      return Utils.string2Number(paramString);
    if (paramXMLColumnNameType.getType() == 3)
      return new Boolean(paramString);
    if (paramXMLColumnNameType.getType() == 2)
    {
      SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(((XMLColumnNameDateType)paramXMLColumnNameType).getFormat());
      return localSimpleDateFormat.parse(paramString);
    }
    return null;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str1;
      String str2 = paramXMLableReader.getTagName();
      if ("CharSet".equals(str2))
        if ((str1 = paramXMLableReader.getElementValue()) != null)
          this.charSet = str1.trim();
      else if ("XPath".equals(str2))
        if ((str1 = paramXMLableReader.getAttr("length")) != null)
        {
          this.xPath = new String[Integer.parseInt(str1.trim())];
          paramXMLableReader.readXMLObject(new XMLReadable(this)
          {
            String tmpV;
            private final XMLTableData this$0;

            public void readXML()
            {
              if ((paramXMLableReader.isChildNode()) && ("XPathElem".equals(paramXMLableReader.getTagName())))
                if ((this.tmpV = paramXMLableReader.getAttr("idx")) != null)
                {
                  int i = Integer.parseInt(this.tmpV.trim());
                  if ((this.tmpV = paramXMLableReader.getElementValue()) != null)
                    XMLTableData.access$000(this.this$0)[i] = this.tmpV.trim();
                }
            }
          });
        }
      else if ("Columns".equals(str2))
        if ((str1 = paramXMLableReader.getAttr("length")) != null)
        {
          this.columns = new XMLColumnNameType[Integer.parseInt(str1.trim())];
          paramXMLableReader.readXMLObject(new XMLReadable(this)
          {
            String tmpV;
            private final XMLTableData this$0;

            public void readXML()
            {
              if ((paramXMLableReader.isChildNode()) && ("ColumnElem".equals(paramXMLableReader.getTagName())))
                if ((this.tmpV = paramXMLableReader.getAttr("idx")) != null)
                {
                  int i = Integer.parseInt(this.tmpV.trim());
                  boolean bool = false;
                  if ((this.tmpV = paramXMLableReader.getAttr("isdate")) != null)
                    bool = new Boolean(this.tmpV.trim()).booleanValue();
                  XMLColumnNameType localXMLColumnNameType = new XMLColumnNameType();
                  paramXMLableReader.readXMLObject(localXMLColumnNameType);
                  XMLTableData.access$100(this.this$0)[i] = localXMLColumnNameType;
                }
            }
          });
        }
      else if ("DataSource".equals(str2))
        this.dataSource = DataXMLUtils.readXMLDataSource(paramXMLableReader);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    int i;
    if (!("UTF-8".equals(this.charSet)))
      paramXMLPrintWriter.startTAG("CharSet").textNode(this.charSet).end();
    if (this.xPath != null)
    {
      paramXMLPrintWriter.startTAG("XPath").attr("length", this.xPath.length);
      for (i = 0; i < this.xPath.length; ++i)
        if (this.xPath[i] != null)
          paramXMLPrintWriter.startTAG("XPathElem").attr("idx", i).textNode(this.xPath[i]).end();
      paramXMLPrintWriter.end();
    }
    if (this.columns != null)
    {
      paramXMLPrintWriter.startTAG("Columns").attr("length", this.columns.length);
      for (i = 0; i < this.columns.length; ++i)
        if (this.columns[i] != null)
        {
          paramXMLPrintWriter.startTAG("ColumnElem").attr("idx", i).attr("isdate", this.columns[i].getType() == 2);
          this.columns[i].writeXML(paramXMLPrintWriter);
          paramXMLPrintWriter.end();
        }
      paramXMLPrintWriter.end();
    }
    if (this.dataSource != null)
      DataXMLUtils.writeXMLDataSource(paramXMLPrintWriter, this.dataSource);
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof XMLTableData))
      return false;
    XMLTableData localXMLTableData = (XMLTableData)paramObject;
    return ((paramObject instanceof XMLTableData) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.dataSource, localXMLTableData.dataSource)) && (ComparatorUtils.equals(this.charSet, localXMLTableData.charSet)) && (ComparatorUtils.equals(this.columns, localXMLTableData.columns)) && (ComparatorUtils.equals(this.xPath, localXMLTableData.xPath)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    XMLTableData localXMLTableData = (XMLTableData)super.clone();
    if (this.columns != null)
    {
      XMLColumnNameType[] arrayOfXMLColumnNameType = new XMLColumnNameType[this.columns.length];
      for (int i = 0; i < this.columns.length; ++i)
        arrayOfXMLColumnNameType[i] = ((XMLColumnNameType)this.columns[i].clone());
      localXMLTableData.setColumns(arrayOfXMLColumnNameType);
    }
    if (this.dataSource != null)
      localXMLTableData.setDataSource((DataSource)this.dataSource.clone());
    if (this.xPath != null)
      localXMLTableData.setXPath((String[])this.xPath.clone());
    return localXMLTableData;
  }

  static String[] access$000(XMLTableData paramXMLTableData)
  {
    return paramXMLTableData.xPath;
  }

  static XMLColumnNameType[] access$100(XMLTableData paramXMLTableData)
  {
    return paramXMLTableData.columns;
  }

  static Object access$200(String paramString, XMLColumnNameType paramXMLColumnNameType)
    throws Exception
  {
    return convertStringValue(paramString, paramXMLColumnNameType);
  }

  private class ReadXMLColumn
    implements XMLReadable
  {
    private Object[] objects;
    private final XMLTableData this$0;

    public ReadXMLColumn(, Object[] paramArrayOfObject)
    {
      this.this$0 = paramXMLTableData;
      this.objects = null;
      this.objects = paramArrayOfObject;
    }

    public void readXML()
    {
      String str1;
      int i;
      if (paramXMLableReader.isChildNode())
      {
        str1 = paramXMLableReader.getTagName();
        for (i = 0; i < XMLTableData.access$100(this.this$0).length; ++i)
          if (XMLTableData.access$100(this.this$0)[i].getName().equals(str1))
          {
            String str2 = paramXMLableReader.getElementValue();
            Object localObject = null;
            if (str2 != null)
              try
              {
                localObject = XMLTableData.access$200(str2.trim(), XMLTableData.access$100(this.this$0)[i]);
              }
              catch (Exception localException)
              {
                throw new RuntimeException("Exception with ColumnName:" + str1 + ", Value:" + str2 + ".\n" + localException.getMessage(), localException);
              }
            this.objects[i] = localObject;
            return;
          }
      }
    }
  }

  private class ReadXMLLayer
    implements XMLReadable
  {
    private int layer;
    private final XMLTableData this$0;

    public ReadXMLLayer(, int paramInt)
    {
      this.this$0 = paramXMLTableData;
      this.layer = -1;
      this.layer = paramInt;
    }

    public void readXML()
    {
      String str;
      if (this.layer < 0)
        return;
      if (paramXMLableReader.isAttr())
      {
        str = XMLTableData.access$000(this.this$0)[this.layer];
        if (!(str.equals(paramXMLableReader.getTagName())))
          this.layer = -1;
      }
      if ((paramXMLableReader.isChildNode()) && (XMLTableData.access$000(this.this$0).length > this.layer + 1))
      {
        str = XMLTableData.access$000(this.this$0)[(this.layer + 1)];
        if (str.equals(paramXMLableReader.getTagName()))
          if (XMLTableData.access$000(this.this$0).length == this.layer + 2)
          {
            Object[] arrayOfObject = new Object[XMLTableData.access$100(this.this$0).length];
            paramXMLableReader.readXMLObject(new XMLTableData.ReadXMLColumn(this.this$0, arrayOfObject));
            this.this$0.row_list.add(arrayOfObject);
          }
          else
          {
            paramXMLableReader.readXMLObject(new ReadXMLLayer(this.this$0, this.layer + 1));
          }
      }
    }
  }
}